﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using System.Collections.Generic;
using Cn.Edwon.Web.Validation;
using System.Reflection;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product {
    public partial class Edit : PageSupport {
        String ProductKey = null;
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            ProductKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(ProductKey)) {
                    FillForm(ProductKey);
                } else {
                    this.txt_ProductNo.Text = biz.GenerateProductNo();
                }
            }
        }

        private void FillForm(String key) {
            VProductEditDetailsEO eo = biz.Load<VProductEditDetailsEO>(key, this.OwnerKey);
            this.txt_ProductNo.Text = eo.ProductNo;
            this.txt_Color.Text = eo.Color;
            this.txt_FactoryDate.Text = (eo.FactoryDate==null)?"":Convert.ToDateTime(eo.FactoryDate).ToString("yyyy-MM-dd");
            this.txt_Height.Text = eo.Height.ToString();
            this.txt_Length.Text = eo.Length.ToString();
            this.txt_Manufacturer.Text = eo.Manufacturer.ToString();
            this.txt_Model.Text = eo.ProductModel.ToString();
            this.txt_ProductDate.Text = (eo.ProductDate == null) ? "" : Convert.ToDateTime(eo.ProductDate).ToString("yyyy-MM-dd");
            this.txt_ProductName.Text = eo.ProductName;
            this.txt_Volume.Text = eo.Volume.ToString();
            this.txt_Weight.Text = eo.Weight.ToString();
            this.txt_Width.Text = eo.Width.ToString();
            this.txt_Tags.Text = eo.Tags;
            this.txt_CategoryName.Text= eo.CategoryName;
            this.hid_CategoryId.Value = eo.CategoryId;
            this.ddl_Material.SelectedValue = eo.MaterialId;
            this.mtxt_Desc.Text = eo.Description;
            this.txt_ProducingArea.Value = eo.ProducingArea;
            this.cbx_Colorable.Checked = (eo.IsColorable == 1);
            this.cbx_Sizable.Checked = (eo.IsSizable == 1);
            this.txt_ClickCount.Text = eo.ClickCount.ToString();
            this.txt_Attechment.Text = eo.Attachment;
            this.ddl_Unit.SelectedValue = eo.UnitKey;
        }
        private void InitForm(){
            ClientBiz.BindCodeList(ref this.ddl_Material, CodeTypeDef.Material.Key, true, null);
            ClientBiz.BindCodeList(ref this.ddl_Unit, CodeTypeDef.ProductUnit.Key, true, null);

            if (!String.IsNullOrEmpty(ProductKey))
                this.btn_save2.Visible = false;

            if (SiteConfig.Product.ProductNoAuto==true) {
                this.txt_ProductNo.Attributes.Add("readonly", "readonly");
                this.txt_ProductNo.CssClass = "input_readonly";
            }

        }

        private TProductEO BindData2EO() {
            TProductEO eo;
            if (String.IsNullOrEmpty(ProductKey)) {
                eo = new TProductEO();
                eo.OwnerKey = this.OwnerKey;
            } else {
                eo = (TProductEO)biz.Load<TProductEO>(ProductKey, this.OwnerKey);
            }
            eo.BarCode = "";
            eo.CategoryId = this.hid_CategoryId.Value;
            eo.Color = this.txt_Color.Text;
            eo.Description = this.mtxt_Desc.Text;
            eo.FactoryDate = CtrlUtil.ParseDateTime(this.txt_FactoryDate.Text);
            eo.Height = CtrlUtil.ParseDecimal(this.txt_Height.Text);
            eo.IsColorable = (this.cbx_Colorable.Checked)?1:0;
            eo.IsSizable = (this.cbx_Sizable.Checked)?1:0;
            eo.IsVerified = 1;
            eo.Length = CtrlUtil.ParseDecimal(this.txt_Length.Text);
            eo.Manufacturer = this.txt_Manufacturer.Text;
            eo.MaterialId = this.ddl_Material.SelectedValue;
            eo.ProducingArea = this.txt_ProducingArea.Value;
            eo.ProductDate = CtrlUtil.ParseDateTime(this.txt_ProductDate.Text);
            eo.ProductModel = this.txt_Model.Text;
            eo.ProductName = this.txt_ProductName.Text;
            eo.Volume = CtrlUtil.ParseDecimal(this.txt_Volume.Text);
            eo.Weight = CtrlUtil.ParseDecimal(this.txt_Weight.Text);
            eo.Width = CtrlUtil.ParseDecimal(this.txt_Width.Text);
            eo.Tags = this.txt_Tags.Text;
            eo.UnitKey = this.ddl_Unit.SelectedValue;
            eo.Attachment = this.txt_Attechment.Text;
            try {
                eo.ClickCount = Convert.ToInt32(this.txt_ClickCount.Text);
            } catch { }
            return eo;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            btn_save2_Click(sender, e);
            Response.Redirect("List.aspx");
        }

        protected void btn_save2_Click(object sender, EventArgs e) {
            Boolean vd = QuickValidator.Execute(Request, this.txt_Color.Parent);
            if (!vd) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TProductEO eo = BindData2EO();

            if (String.IsNullOrEmpty(ProductKey)) {
                if (biz.IfProductNoExist(this.txt_ProductNo.Text)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "产品编号已经存在，请重新提交");
                    this.txt_ProductNo.Text = biz.GenerateProductNo();
                    return;
                } else {
                    eo.ProductNo = this.txt_ProductNo.Text;
                }
                eo.CreateDate = eo.LastDate = DateTime.Now;
                eo.CreateOperator = eo.UpdateOperator = (this.Actor != null) ? this.Actor.ID : "unkown";
                eo = (TProductEO)biz.Save<TProductEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("产品", eo.ProductName, this.Actor, Request);
                Response.Redirect("EditAttr.aspx?id=" + eo.ProductId);
            } else {
                if (eo.ProductNo!=this.txt_ProductNo.Text && biz.IfProductNoExist(this.txt_ProductNo.Text)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "产品编号已经存在，请重新提交");
                    this.txt_ProductNo.Text = biz.GenerateProductNo();
                    return;
                } else {
                    eo.ProductNo = this.txt_ProductNo.Text;
                }
                eo.LastDate = DateTime.Now;
                eo.UpdateOperator = (this.Actor != null) ? this.Actor.ID : "unkown";
                biz.Update<TProductEO>(eo);
                SiteInfo.Send(Page, TxtDef.SUCCEED, "保存成功");
            }
        }
    }
}
